package com.ssm.module.user.controller;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ssm.module.user.entity.User;
import com.ssm.module.user.service.UserService;

/**
 * @author Administrator
 *	用户控制器
 */
@Controller
@RequestMapping("/user")
public class UserController {
	@Autowired
	private UserService userService;
	
	@ModelAttribute
	public User get() {
		return new User();
	}
	
	//请求登录页面
	@RequestMapping("/login")
	public String login() throws Exception {
		return "user/login";
	}
	
	//请求登录
	@RequestMapping("/loginSubmit")
	@ResponseBody
	public String loginSubmit(@Valid @ModelAttribute("user") User user,BindingResult bindingResult, HttpSession session) throws Exception {
		/*if (bindingResult.hasErrors()) {
			return "user/login";
		}*/
		return userService.checkUser(user,session);
	}
	
	//退出
	@RequestMapping("/logout")
	public String logout(HttpSession session) throws Exception {
		session.invalidate();
		//重定向到列表页
		return "redirect:login";
	}
}
